package com.entfrm.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entfrm.base.api.R;
import com.entfrm.base.constant.AppConstants;
import com.entfrm.log.annotation.SysLog;

import com.entfrm.system.entity.MessageType;

import com.entfrm.system.service.MessageTypeService;

import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@AllArgsConstructor
@RequestMapping(AppConstants.APP_SYSTEM + "/message")
public class MessageController {
    private final MessageTypeService messageTypeService;
    private QueryWrapper<MessageType> getQueryWrapper(MessageType messageType) {
        return new QueryWrapper<MessageType>()
                .like(StrUtil.isNotBlank(messageType.getMessageType()), "message_type", messageType.getMessageType())
                .orderByAsc("create_time");
    }


    @GetMapping("/list")
    public R list(Page page, MessageType messageType) {
        IPage<MessageType> messageTypeIPage = messageTypeService.page(page, getQueryWrapper(messageType));
        return R.ok(messageTypeIPage.getRecords(), messageTypeIPage.getTotal());
    }
    @SysLog("新增消息")
    @PostMapping("/save")
    public R save(@Validated @RequestBody MessageType messageType) {
        messageTypeService.save(messageType);
        return R.ok();
    }

    @SysLog("消息类型修改")
    @PutMapping("/update")
    public R update(@Validated @RequestBody MessageType messageType) {
       messageTypeService.updateById(messageType);
        return R.ok();
    }


}
